<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* loginHandler
*
* Encapsulates the Login Routines -- Must be instanced for obvious reasons
* 
*/

define("TRY_LOAD_ACCOUNT", 		0);
define("CREATE_ACCOUNT",   		1);
define("GET_EMAIL_ADDR",		2);
define("GET_PASSWORD_1ST",		3);
define("GET_PASSWORD_2ND",		4);
define("SHOW_USER_CHARACTERS", 	5);
define("CHARACTER_LOGIN",		6);
define("CHARACTER_VERIFY",		7);

class loginHandler {

	public static $login_sessions = array();
	public $connector;
	public $proposedName;
	public $currentStep;
	
	public $useraccount;
	public $characters;
	public $tmp_username;
	public $tmp_password;
	public $tmp_email;
	public $proposedCharName;

	function __construct($connector) {
		$this->connector = $connector;
		$this->currentStep = TRY_LOAD_ACCOUNT;
	}

	public static function welcome($connector) {
		$connector->send(strPackager::package("KUSHT",GAME_TITLE));
		$connector->send(strPackager::package("",GENERAL_MSG));
		$connector->send(strPackager::package("The MIT License &#169; ANDREW TUTT, MMX.",SAXON_LEVEL_2));
		$connector->send(strPackager::package("[[...and <em>many</em> others.|help copyright holders]]",NO_CLEAR));
		// We're dealing with a pure connector, so this is manually styled
		$connector->send(strPackager::package("Give us a name &raquo;",GENERAL_MSG));
	}
	
	public function login($msg) {		
		switch ($this->currentStep) {
			case TRY_LOAD_ACCOUNT:		// They have not typed anything yet OR they've gone back to the beginning
				$this->try_load_account($msg);
				return;
			case CREATE_ACCOUNT:
				$this->create_new_account($msg);
				return;
			case GET_EMAIL_ADDR:
				$this->get_email_address($msg);
				return;
			case GET_PASSWORD_1ST:
				$this->get_password_first($msg);
				return;
			case GET_PASSWORD_2ND:
				$this->get_password_second($msg);
				return;
			case SHOW_USER_CHARACTERS:
				if($this->useraccount->password != $msg) {
					$this->connector->send(strPackager::package("Wrong Password.",GENERAL_MSG));
					$this->connector->server->disconnect($this->connector->socket);
					return;
				}
				$this->show_user_characters();
				return;
			case CHARACTER_LOGIN:
				$this->character_login($msg);
				return;
			case CHARACTER_VERIFY:
				$this->character_verify($msg);
				return;
		}
	}
	
	public function try_load_account($msg) {
		$useraccount = dbHandler::loadUserAccount($msg);
		// Name not recognized -> Create a name
		if($useraccount===false) {
			if(!isset($this->proposedName)) { $this->proposedName = $msg; }
			$this->connector->send(strPackager::package("That account isn't on file. 
			Type it again to create it, or type another name to try again. &raquo;",GENERAL_MSG));
			$this->currentStep = CREATE_ACCOUNT;
			return;
		} else {		
			if(get_obj_by_field_value($useraccount->id,"owner_id",World::$char_list)) {
				$this->connector->send(strPackager::package("A character from 
					that account is already connected and playing. Try again. &raquo;",GENERAL_MSG));	
					$this->currentStep = TRY_LOAD_ACCOUNT;
					return;		
			} else {
				$this->useraccount = $useraccount;
				$this->connector->send(strPackager::package("Password &raquo;",GENERAL_MSG));
				$this->currentStep = SHOW_USER_CHARACTERS;
				return;
			}
		}
	}
	
	public function create_new_account($msg) {
		if($this->proposedName == $msg) {
			$this->tmp_username = $msg;
			$this->connector->send(strPackager::package("Alright " . $msg . " it is. Don't forget, it's case sensitive.",GENERAL_MSG));
			$this->connector->send(strPackager::package("Let us know your email address? (no spam, we promise) &raquo;",GENERAL_MSG));
			$this->currentStep = GET_EMAIL_ADDR;
		} else {
			$this->currentStep = TRY_LOAD_ACCOUNT;
			$this->proposedName = $msg;
			$this->login($msg);
			return;
		}
	}
	
	public function get_email_address($msg) {
		$this->tmp_email = $msg;
		$this->connector->send(strPackager::package("And now a password &raquo;",GENERAL_MSG));
		$this->currentStep = GET_PASSWORD_1ST;
		return;
	}
	
	public function get_password_first($msg) {
		$this->tmp_password = $msg;
		$this->connector->send(strPackager::package("Retype it &raquo;",GENERAL_MSG));
		$this->currentStep = GET_PASSWORD_2ND;
		return;
	}
	
	public function get_password_second($msg) {
		if ($this->tmp_password == $msg) {
			dbHandler::saveUserAccount($this->tmp_username,$this->tmp_password,$this->tmp_email);
			$this->useraccount->id = $this->tmp_username;
			$this->connector->send(strPackager::package("Got it. Congrats, you've got an account.",GENERAL_MSG));
			$this->connector->send(strPackager::package("You don't have any character's yet! Type in a name to create a character. &raquo;",GENERAL_MSG));
			$this->currentStep = CHARACTER_LOGIN;
		} else {
			$this->connector->send(strPackager::package("Your passwords don't match. Try again. &raquo;",GENERAL_MSG));
			$this->currentStep = GET_PASSWORD_1ST;
		}
	}
	
	public function show_user_characters() {		
		$characters = dbHandler::loadUserCharacters($this->useraccount->id);
		
		if($characters && $this->connector) {
			$this->characters = array();
			$this->connector->send(strPackager::package("Your characters are:",GENERAL_MSG));
			foreach($characters as $char) {
				$tmp = new UserCharacter();
				$tmp->get($char['id']);
				array_push($this->characters, $tmp);
				$this->connector->send(strPackager::package($tmp->name,GENERAL_MSG));
			}
			$this->connector->send(strPackager::package("Login as one of your characters, or type in a new name to create a new one:",GENERAL_MSG));
			$this->currentStep = CHARACTER_LOGIN;
		} else {
			$this->connector->send(strPackager::package("You don't have any character's yet! Type in a name to create a character. &raquo;",GENERAL_MSG));
			$this->currentStep = CHARACTER_LOGIN;
			return;
		}
	}
	
	public function character_login($msg) {
		$test = get_obj_by_field_value($msg,"name",$this->characters);
		if($test) {
			$this->move_to_game($test);
		// This forces unique usernames
		} elseif(dbHandler::charExists($msg)) {
			$this->connector->send(strPackager::package("That character name already exists. Try another one instead.",GENERAL_MSG));
			$this->show_user_characters();	
		} else {
			$this->proposedCharName = $msg;
			$this->connector->send(strPackager::package("You wish to be known as " . $msg . "? Is that right? (Yes / No) &raquo;",GENERAL_MSG));
			$this->currentStep = CHARACTER_VERIFY;
		}

	}
	
	public function move_to_game($char) {
		$this->connector->user = $char;
		$char->connector = $this->connector;
		
		// Temporary until we have more sophisticated databasing
		$this->connector->loggedin = true;
		$this->connector->user->setposition(STANDING);
		$this->connector->user->set(FEMALE);
		
		// Add to World and Room		
		World::add_char_to_world($this->connector->user);
		World::$room_list[0]->add_char($this->connector->user);
		$this->connector->user->in_room(World::$room_list[0]);
		
		// And load the client...
		$this->connector->user->loadClient();
		
		// Delete from Memory
		array_del_elem(loginHandler::$login_sessions, $this);
		unset($this);
	}
	
	public function character_verify($msg) {
		if(stristr("yes",$msg)) { $this->create_character(); return; }
		else { $this->show_user_characters(); }
	}
	
	public function create_character() {
		$this->connector->user = new UserCharacter();
		$this->connector->user->connector = $this->connector;
		$this->connector->user->owner_id = $this->useraccount->id;
		
		// Give some basic information (temporary, this will eventually be made more sophisticated)
		$this->connector->user->name = $this->proposedCharName;
		$this->connector->user->description ="some guy";
		$this->connector->user->image ="genericman1.jpg";
		$this->connector->loggedin = true;
		$this->connector->user->setposition(STANDING);
		$this->connector->user->set(MALE);
		
		// Add to World and Room		
		World::add_char_to_world($this->connector->user);
		World::$room_list[0]->add_char($this->connector->user);
		$this->connector->user->in_room(World::$room_list[0]);
		
		// Finally, save the character...
		$this->connector->user->put();		
		
		// And load the client...
		$this->connector->user->loadClient();
			
		// Delete from Memory
		array_del_elem(loginHandler::$login_sessions, $this);
		unset($this);
	}

}




?>
